﻿/*----------------------------------------------------------------
// Copyright (C) 2011 周士祎
// 版权所有。 
//
// 文件名：DataAccessMenu
// 文件功能描述：左边树菜单数据层
//
// 
// 创建标识：2012-01-03 周士祎
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DbAdapter.sqldbAdapter;

namespace DAO.WaiGaoQiao
{
    public class DataAccessMenu
    {
        /// <summary>
        /// 根据条件获取菜单
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public DataSet GetMenuBy(string condition)
        {
            sqldb db = new sqldb();
            string query = "select SID,ModularId,URL,Title,Description,UserList,NodeID,PNodeID,ImageUrl FROM "
                          +"Tbt_SitMap "+condition;
            DataSet ds = db.ExecuteDataset(GetConntionString.conntionstring(),CommandType.Text,query);            
            return ds;            
        }
        /// <summary>
        /// 绑定菜单到用户
        /// </summary>
        /// <param name="logid"></param>
        /// <param name="meid"></param>
        /// <returns></returns>
        public int BindMenuToRole(string roleid, int meid)
        {
            sqldb db = new sqldb();
            string query = "if Not Exists(select * from Tbt_RoleAndMenu WHERE RoleId = '" + roleid.Trim()
                          + "' and MenuId = " + meid.ToString() + ") insert into Tbt_RoleAndMenu(RoleId,MenuId) "
                          + "Values('" + roleid.Trim() + "'," + meid.ToString() + ")";
            return db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        /// <summary>
        /// 删除菜单绑定
        /// </summary>
        /// <param name="logid"></param>
        /// <param name="meid"></param>
        /// <returns></returns>
        public int NotBindMenuToRole(string roleid, int meid)
        {
            sqldb db = new sqldb();
            string query = "delete from Tbt_RoleAndMenu WHERE RoleId = '" + roleid.Trim()
                          + "' and MenuId = " + meid.ToString();
            return db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
                 
        }
        /// <summary>
        /// 获取已经绑定的菜单
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public DataSet GetBindedMenu(string roleid)
        {
            sqldb db = new sqldb();
            string query = "select RoleId,MenuId from Tbt_RoleAndMenu where RoleId = '" + roleid + "'";
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }
        /// <summary>
        /// 先清理绑定的角色菜单
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int ClearBindMenuToRole(string roleid)
        {
            sqldb db = new sqldb();
            string query = "delete from Tbt_RoleAndMenu where RoleId = '" + roleid + "'";
            return db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
        }
    }
}
